package application;

import vnu.ACLMessageX;
import vnu.AgentInfo;
import vnu.JobAgent;
import jade.content.lang.sl.SLCodec;
import jade.core.AID;
import jade.core.Agent;
import jade.core.PlatformID;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;

import vocabulary.MyMapVoca;
public class MobileAgent extends JobAgent implements MyMapVoca {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	private int count=0;
	@Override
	protected void afterMove() {
		
		// TODO Auto-generated method stub
		super.afterMove();
		System.out.println("moved successfully...." );
		System.out.println("My LocalName: " + getAID().getLocalName());
		System.out.println("My Name: " + getAID().getName());
		System.out.println("My Address: " + getAID().getAddressesArray()[0]);
		count=0;
		addBehaviour(new RegisterInDF(this));
		
	}
	@Override
	protected void beforeMove() {
		// TODO Auto-generated method stub
		super.beforeMove();
		System.out.println("beforemove"  + getAID().toString());
	}
	private SLCodec codec = new SLCodec();
	@Override
	protected void setup() {
		super.setup();
	
		System.out.println("Hello World. I am an agent!");
		System.out.println("My LocalName: " + getAID().getLocalName());
		System.out.println("My Name: " + getAID().getName());
		System.out.println("My Address: " + getAID().getAddressesArray()[0]);
	//addBehaviour();
	addBehaviour(new RecievedMess());
	
	}
class RegisterInDF extends OneShotBehaviour{
	private static final long SerialVersionUID=1L;
	
	RegisterInDF(Agent a){
		super(a);
	}
	
	public void action(){
		getContentManager();	
	}	
}
	private class MyTickerBehaviour extends TickerBehaviour {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		Agent agent;
		// long interval;
		int counter;
		public MyTickerBehaviour(Agent agent, long interval) {
			super(agent, interval);
			this.agent = agent;
			counter=0;
			System.out.println("State..."+getAgentState().getName());
		}
		@Override
		protected void onTick() {
		if(counter == 5){
				// move out
				AID remoteAMS = new AID("amm@toshiba-PC",AID.ISGUID);
				remoteAMS.addAddresses("http://toshiba-PC:7778/acc");
				PlatformID destination = new PlatformID(remoteAMS);
				agent.doMove(destination);
		}
		if (counter <= 10)
			System.out.println(counter++);
		
		}

	}

class RecievedMess extends CyclicBehaviour{
	public void action(){
		jade.lang.acl.ACLMessage msg = receive();
		if ( msg != null){
			count++;
			try {
				if(msg.getSender().getLocalName().equals(AGENT_MANAGER) ){
				ACLMessageX content =(ACLMessageX) msg.getContentObject();
				System.out.println("[Mobile Agent] Nhan message voi noi dung " + content.getContent());
				System.out.println("Ngư�?i gửi " + content.getSource());
				switch (content.getPerformative()) {
				case ACLMessage.REQUEST:
					ACLMessage reply = new ACLMessage(ACLMessage.INFORM);
					reply.addReceiver(content.getSource());
					reply.setContent("pong"+content.getContent());
					send(reply);
					break;
				case ACLMessage.INFORM:
					
				default:
					break;
				}
				}
				else {
					ACLMessage reply = msg.createReply();
					reply.setContent("pong");
					send(reply);
				}
				
			} catch (UnreadableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		if(count==5) addBehaviour(new MyTickerBehaviour(myAgent, 1000));
		block();
	
	}
}
}
